package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.AuxiliaryCategoryTDao;
import com.sykj.why.mongo.document.entity.AuxiliaryCategoryT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.AuxiliaryCategoryTService;
import com.sykj.why.mongo.vo.entity.AuxiliaryCategoryTree;
import com.sykj.why.util.DateTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/12
 * @desc 辅助分类业务实现层
 */

@Service("AuxiliaryCategoryTService")
public class AuxiliaryCategoryTServiceImpl extends BasicServiceImpl<AuxiliaryCategoryT> implements AuxiliaryCategoryTService {

    @Autowired
    private AuxiliaryCategoryTDao auxiliaryCategoryTDao;

    /**
     * 添加辅助分类
     * @param auxiliaryCategoryT
     */
    @Override
    public void saveAuxiliaryCategory(AuxiliaryCategoryT auxiliaryCategoryT) {
        //验证名称是否重复
        verifyName(auxiliaryCategoryT);
        //排序
        long count = auxiliaryCategoryTDao.count(AuxiliaryCategoryT.class, false);
        auxiliaryCategoryT.setSort((int) count);
        //设置时间
        auxiliaryCategoryT.setCreateTime(DateTool.getSystemTime());
        auxiliaryCategoryT.setUpdateTime(DateTool.getSystemTime());
        //保存
        auxiliaryCategoryTDao.save(auxiliaryCategoryT);
    }

    /**
     * 删除辅助分类
     * @param id
     */
    @Override
    public void removeAuxiliaryCategory(String id) {
        //通过Id查询
        AuxiliaryCategoryT auxiliaryCategoryT = findById(id);
        //逻辑删除
        auxiliaryCategoryTDao.remove(auxiliaryCategoryT);
    }

    /**
     * 修改辅助分类
     * @param auxiliaryCategoryT
     */
    @Override
    public void updateAuxiliaryCategory(AuxiliaryCategoryT auxiliaryCategoryT) {
        //通过id查询
        AuxiliaryCategoryT auxiliaryCategory = findById(auxiliaryCategoryT.getId());
        //重新赋值
        //验证名称是否重复
        verifyName(auxiliaryCategoryT);
        auxiliaryCategory.setCategoryName(auxiliaryCategoryT.getCategoryName());
        auxiliaryCategory.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        auxiliaryCategoryTDao.saveOrupdate(auxiliaryCategory);
    }

    /**
     * 启用/禁用辅助分类
     * @param id
     * @param flag
     */
    @Override
    public void disableOrEnable(String id, boolean flag) {
        //通过id查询
        AuxiliaryCategoryT auxiliaryCategoryT = findById(id);
        //修改状态
        auxiliaryCategoryTDao.disableOrEnable(auxiliaryCategoryT,flag);
    }

    /**
     * 验证辅助分类名称是否重复
     * @param auxiliaryCategoryT
     */
    @Override
    public void verifyName(AuxiliaryCategoryT auxiliaryCategoryT) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").ne(auxiliaryCategoryT.getId()).
                and("pid").is(auxiliaryCategoryT.getPid()).
                and("categoryName").is(auxiliaryCategoryT.getCategoryName()).and("removed").is(false));
        AuxiliaryCategoryT auxiliaryCategory = auxiliaryCategoryTDao.findOne(query,AuxiliaryCategoryT.class);
        if (auxiliaryCategory != null) {
            throw new WhyException(ResultEnum.REPEAT);
        }
    }

    /**
     * 通过id查询辅助分类
     * @param id
     * @return
     */
    @Override
    public AuxiliaryCategoryT findById(String id) {
        //通过id查询未删除的记录
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        AuxiliaryCategoryT auxiliaryCategoryT = auxiliaryCategoryTDao.findOne(query,AuxiliaryCategoryT.class);
        if (auxiliaryCategoryT!=null){
            return auxiliaryCategoryT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 查询所有辅助分类
     * @return
     */
    @Override
    public List<AuxiliaryCategoryT> findAll() {
        //查询未删除和未禁用的所有记录
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true));
        List<AuxiliaryCategoryT> list = auxiliaryCategoryTDao.findAll(query,AuxiliaryCategoryT.class);
        return list;
    }

    /**
     * 通过父级Id查询子级分类列表
     * @param pid
     * @return
     */
    @Override
    public List<AuxiliaryCategoryT> findByPid(String pid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true));
        query.addCriteria(Criteria.where("pid").is(pid));
        List<AuxiliaryCategoryT> list = auxiliaryCategoryTDao.findAll(query,AuxiliaryCategoryT.class);
        return list;
    }

    /**
     * 前端树状图
     * @return
     */
    @Override
    public List<AuxiliaryCategoryTree> findTree(String pid) {
        List<AuxiliaryCategoryT> list = findByPid(pid);
        List<AuxiliaryCategoryTree> voList = new ArrayList<>();
        if (list!=null){
            for (AuxiliaryCategoryT auxiliaryCategoryT : list){
                AuxiliaryCategoryTree vo = new AuxiliaryCategoryTree();
                BeanUtils.copyProperties(auxiliaryCategoryT, vo);
                vo.setValue(auxiliaryCategoryT.getId());
                vo.setLabel(auxiliaryCategoryT.getCategoryName());
                if (findById(auxiliaryCategoryT.getId())!=null){
                    vo.setChildren(findTree(auxiliaryCategoryT.getId()));
                }
                voList.add(vo);
            }
        }
        return voList;
    }


}
